package com.beidu.lottery.call.entity.query;

import com.beidu.lottery.call.entity.common.PageQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 中奖记录分页查询条件
 *
 * @author 北渡
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ActivityAwardRecordsQuery extends PageQuery implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页码
     */
    @Min(value = 1, message = "页码不能小于1")
    private Integer pageNum;

    /**
     * 每页数量
     */
    @Min(value = 1, message = "每页数量不能小于1")
    private Integer pageSize;

    /**
     * 活动ID
     */
    @NotNull(message = "活动ID不能为空")
    private Integer id;

    /**
     * 随机页码
     * <p>
     * 页码数量算法：假设pageSize=10, total=21, 页码数返回2(最大化满足分页数量), total小于分页行数则返回1
     * 随机分页算法：total/pageSize做为随机界限值
     *
     * @param total 总条数
     * @author 北渡
     */
    public void randomPageNum(int total) {
        int page = total < this.getPageSize() ? 1 : total / this.getPageSize();// 计算最大页数
        int randomNum = ThreadLocalRandom.current().nextInt(page);// 随机数
        this.pageNum = randomNum * this.getPageSize();// 偏移量
    }

}
